package com.woniuxy.woniubook.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.woniubook.entity.Book;
import com.woniuxy.woniubook.entity.User;
import com.woniuxy.woniubook.service.BookService;
import com.woniuxy.woniubook.service.CartService;
import com.woniuxy.woniubook.service.UserService;
import com.woniuxy.woniubook.uitl.CookieUtil;
import com.woniuxy.woniubook.uitl.JwtUtils;
import com.woniuxy.woniubook.vo.CartItemVo;
import com.woniuxy.woniubook.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller // @Controller + @RepsonseBody
@RequestMapping("/cart")
public class CartController {

    @Autowired
    UserService userService ;

    @Autowired
    CartService cartService ;

    @RequestMapping("/addToCart")
    @ResponseBody
    public String addToCart(Long bookId, HttpServletRequest request){
        System.out.println("bookId="+bookId);
        User user = this.getUser(request);
        return cartService.addToCart(user,bookId);
    }

    @RequestMapping("/myCart")
    @ResponseBody
    public CartVo myCart(HttpServletRequest request) {
        //获取 用户的对象
        User user = this.getUser(request);
        CartVo cartVo = cartService.getMyCart(user.getId());
        return cartVo ;
    }

    @RequestMapping("/myBuyCart")
    @ResponseBody
    public CartVo myBuyCart(@RequestParam(value = "bookIds[]") Long[] bookIds,HttpServletRequest request){
        User user = this.getUser(request);
        CartVo cartVo = cartService.getMyCart(user.getId());
        List<CartItemVo> list = new ArrayList<>();
        BigDecimal all = new BigDecimal("0.0");
        for(CartItemVo cv :cartVo.getCarts()){
            for (Long bid :bookIds){
                if (cv.getBookId().equals(bid)) {
                    list.add(cv);
                    all = all.add(cv.getSumPrice());
                }
            }
        }
        cartVo.setCarts(list);
        cartVo.setSumPrice(all);
        return  cartVo ;
    }

    @RequestMapping("/selectMyCart")
    @ResponseBody
    public BigDecimal selectMyCart(@RequestParam(name = "idArr[]") int[] idArr,HttpServletRequest request){
        User user = this.getUser(request);
        CartVo cartVo = cartService.getMyCart(user.getId());
        BigDecimal all = new BigDecimal("0.0") ;
        List<CartItemVo> carts = cartVo.getCarts();
        for (CartItemVo cv :carts) {
            for (int id : idArr) {
                if (cv.getBookId() == id) {
                    all = all.add(cv.getSumPrice());
                }
            }
        }

        return  all ;
    }


    private User getUser(HttpServletRequest request){
        // 获取 用户的ID
        String user_token = CookieUtil.getToken(request, "user_token");
        String account = JwtUtils.getAccount(user_token);
        //查询用户对象
        User user = userService.getUserByName(account);
        return user ;
    }

    @RequestMapping("/updateCount")
    @ResponseBody
    public String updateCount(Long bookId ,int bookCount,HttpServletRequest request){
        System.out.println("bookId="+ bookId +", bookCount="+bookCount);
        String s = "" ;
        if(bookCount < 1 || bookCount > 100){
            s = "购买数量不能小于0，也不能超过100!" ;
        }else {
            User user = this.getUser(request);
            s = cartService.modifyCount(user.getId(), bookId, bookCount);
        }
        return s ;
    }

    @RequestMapping("/cal")
    public String cal(int[] idArr, Map map) throws JsonProcessingException {
        for (int i :idArr){
            System.out.println(i);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        map.put("bookIds",objectMapper.writeValueAsString(idArr));
        return "submitCart" ;
    }

}
